package com.loan.controller;


import com.loan.entity.Admin;
import com.loan.entity.Result;
import com.loan.mapper.AdminMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminMapper adminMapper;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(String adminName, String adminPassword){

        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("adminName",adminName);
        columnMap.put("adminPassword",adminPassword);
        List<Admin> adminList = adminMapper.selectByMap(columnMap);

        Result result = new Result();
        if (adminList.size()>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/getAdminList",method = RequestMethod.POST)
    public List<Admin> getAdminList(){
        List<Admin> adminList = adminMapper.selectList(null);
        return adminList;
    }

    @RequestMapping(value = "/updateAdmin",method = RequestMethod.POST)
    public Result updateAdmin(Admin admin){
        int count =0;
        if (admin.getAdminId()==null){
            adminMapper.insert(admin);
        }
        if (admin.getAdminId()!=null){
            adminMapper.updateById(admin);
        }

        Result result = new Result();
        result.setSuccess(true);
        return result;
    }
    @RequestMapping(value = "/deleteAdmin",method = RequestMethod.POST)
    public Result deleteAdmin(@Param("adminId") Integer adminId){
        System.out.println(adminId);
        int count = adminMapper.deleteById(adminId);
        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

}

